package android.sdk;

import java.util.UUID;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
import android.view.MotionEvent;

public class SDKClientService extends Service {

	private NetworkController network;
	private SensorController sensor;
	private TouchScreenController touchscreen;
	
    public static final String ON_RESUME = "android.sdk.ON_RESUME";
    public static final String ON_PAUSE = "android.sdk.ON_PAUSE";
    public static final String CONNECT = "android.sdk.CONNECT";
    public static final String DISCONNECT = "android.sdk.DISCONNECT";
    public static final String REGISTER_VIEW = "android.sdk.REGISTER_VIEW";
    public static final String CLICK_EVENT = "android.sdk.CLICK_EVENT";
    public static final String LONGCLICK_EVENT = "android.sdk.LONGCLICK_EVENT";
    public static final String TOUCH_EVENT = "android.sdk.TOUCH_EVENT";
    
    public static final String IP = "android.sdk.IP";
    public static final String PORT = "android.sdk.PORT";
    public static final String TIMEOUT = "android.sdk.TIMEOUT";
    public static final String ID = "android.sdk.ID";
    public static final String NAME = "android.sdk.NAME";
    public static final String MOTION_EVENT = "android.sdk.MOTION_EVENT";
    
    private SharedPreferences preferences;
	private String deviceId;
    public static final String PREFERENCES = "android.sdk";
    
	@Override
	public void onCreate(){
		super.onCreate();
        
		preferences = getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
		deviceId = preferences.getString("deviceId", "null");
		if(deviceId.equals("null"))
			deviceId = generateRandomId();
		
        network = new NetworkController(this);
        sensor = new SensorController(this, network);
        touchscreen = new TouchScreenController(this, network);
	}
	

	public void onResume(){
        sensor.onResume();
	}
	
	public void onPause(){
        sensor.onPause();
	}
	
	@Override
	public void onDestroy(){
        super.onDestroy();
        Log.i("Tesis", "onDestroy() Servicio");
        
        sensor.unregisterAccelerometerEvent();
        network.disconnect();
	}
	
	@Override 
	public int onStartCommand(Intent intent, int flags, int startId) {
		if(intent.getAction() != null){
			if (intent.getAction().equals(ON_RESUME)){
				this.onResume();
			} else if(intent.getAction().equals(ON_PAUSE)){
				this.onPause();
			} else if(intent.getAction().equals(CONNECT)){
				this.network.connect(intent.getStringExtra(IP), intent.getIntExtra(PORT,51560), intent.getIntExtra(TIMEOUT, 10000));
			} else if(intent.getAction().equals(DISCONNECT)){
				this.network.disconnect();
			} else if(intent.getAction().equals(REGISTER_VIEW)) {
				this.touchscreen.registerView(intent.getIntExtra(ID, 0xffffffff), intent.getStringExtra(NAME));
			} else if(intent.getAction().equals(CLICK_EVENT)) {
				this.touchscreen.onClick(intent.getIntExtra(ID, 0));
			} else if(intent.getAction().equals(LONGCLICK_EVENT)) {
				this.touchscreen.onLongClick(intent.getIntExtra(ID, 0));
			} else if(intent.getAction().equals(TOUCH_EVENT)) {
				this.touchscreen.onTouch(intent.getIntExtra(ID, 0xffffffff), (MotionEvent)intent.getParcelableExtra(MOTION_EVENT));
			}
		}
		return START_STICKY;
	}		
	
	
		
	public void registerAccelerometerEvent() {
		this.sensor.registerAccelerometerEvent();
	}


	public void unregisterAccelerometerEvent() {
		this.sensor.unregisterAccelerometerEvent();
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}


	public void disconnect() {
		this.unregisterAccelerometerEvent();
		
		Intent intent = new Intent(SDKClientActivity.UNREGISTER_ALL_EVENTS);
		sendBroadcast(intent);
	}
    
	private String generateRandomId(){
		String id = Long.toHexString(UUID.randomUUID().getMostSignificantBits());
		SharedPreferences.Editor editor = preferences.edit();
		editor.putString("deviceId", id);
		editor.commit();
		Log.i("Tesis", "Android_ID inventado ==> " + id);
		return id;
	}

	public String getDeviceId() {
		return deviceId;
	}


	public void setAccelerometerRate(int rate) {
		sensor.setAccelerometerRate(rate);
	}


	public void connectionEstablished() {
		Intent intent = new Intent(SDKClientActivity.CONNECTION_ESTABLISHED);
		sendBroadcast(intent);
	}

	public void registerOnClickEvent(int id) {
		Intent intent = new Intent(SDKClientActivity.REGISTER_CLICK_EVENT);
		intent.putExtra(SDKClientActivity.VIEW_ID, id);
		sendBroadcast(intent);
	}


	public void unregisterOnClickEvent(int id) {
		Intent intent = new Intent(SDKClientActivity.UNREGISTER_CLICK_EVENT);
		intent.putExtra(SDKClientActivity.VIEW_ID, id);
		sendBroadcast(intent);
	}

	public void registerOnLongClickEvent(int id) {
		Intent intent = new Intent(SDKClientActivity.REGISTER_LONGCLICK_EVENT);
		intent.putExtra(SDKClientActivity.VIEW_ID, id);
		sendBroadcast(intent);
	}


	public void unregisterOnLongClickEvent(int id) {
		Intent intent = new Intent(SDKClientActivity.UNREGISTER_LONGCLICK_EVENT);
		intent.putExtra(SDKClientActivity.VIEW_ID, id);
		sendBroadcast(intent);
	}
	
	public void registerOnTouchEvent(int id) {
		Intent intent = new Intent(SDKClientActivity.REGISTER_TOUCH_EVENT);
		intent.putExtra(SDKClientActivity.VIEW_ID, id);
		sendBroadcast(intent);
	}


	public void unregisterOnTouchEvent(int id) {
		Intent intent = new Intent(SDKClientActivity.UNREGISTER_TOUCH_EVENT);
		intent.putExtra(SDKClientActivity.VIEW_ID, id);
		sendBroadcast(intent);
	}


	public void connectionFailed(String message) {
		Intent intent = new Intent(SDKClientActivity.CONNECTION_FAILED);
		intent.putExtra(SDKClientActivity.CONNECTION_FAILED_REASON, message);
		sendBroadcast(intent);
	}
}
